/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package MyStatementExecution.MyUpdate;

import MyDataContainer.MyQuery;
import MyDataContainer.MyTable;
import MyDataType.MyData;
import MyStatementExecution.MySelect.MySelectStatement;
import MyStatementExecution.MyStatement;
import MyStatementExecution.MyStatementKind;
import StringJoinerCutter.StringJoinerCutter;

/**
 *
 * @author Welcome
 */
public class MyUpdateStatement extends MyStatement{

    public MyUpdateStatement() {
        myStatementKind = MyStatementKind.UPDATE;
    }
    
    
    

    @Override
    public MyStatementKind getKind() {
        return myStatementKind;
    }

    @Override
    public void executeStatement() {
        try
        {
            String tableName = StringJoinerCutter.getStringInMidle(statementString, "update", "set").trim();
            MyTable targetTable = databaseHandler.getTable(tableName);
            
            String columnString = StringJoinerCutter.getStringInMidle(statementString, "set", "where").trim();
            String columnName="";
            String pattern="";
            String[] strList = columnString.split("=");
            if(strList.length>1)
            {
                columnName=strList[0].trim();
                pattern=strList[1].trim();
                Character x = pattern.charAt(0);
                if(x.compareTo('\'')==0)
                    pattern=pattern.substring(1,pattern.length()-1);
            }
            
            int columnPosition = targetTable.getColumnTitles().getPosition(columnName);
            
            String whereString = StringJoinerCutter.getStringInMidle(statementString, "where", "").trim();
            if(whereString.equals(""))
            {
               //Set toan bo column thanh nhu vay
                for(int i=0; i<targetTable.getSize();i++)
                {
                    MyData tempData = targetTable.getRowAt(i).getDataAt(columnPosition);
                    tempData.setContent(pattern);
                }
                //targetTable.showData();
               return;
            }
            
            String newTempSelect = "select * from "+tableName+" where "+StringJoinerCutter.getStringInMidle(statementString, "where", "");
            MySelectStatement selectStatement = new MySelectStatement(databaseHandler, newTempSelect);
           
            MyQuery tempQuery = new MyQuery(selectStatement.executeSelectStatement());
           
           for(int i=0; i<targetTable.getSize();i++)
           {
               for(int j=0; j<tempQuery.getSize();j++)
                   if(targetTable.getRowAt(i).isEqual(tempQuery.getRowAt(j)))
                   {
                       //Set data here
                       MyData tempData = targetTable.getRowAt(i).getDataAt(columnPosition);
                       tempData.setContent(pattern);
                   }
           }
           
           databaseHandler.WriteDatabase(databaseHandler.getPathFile());
           targetTable.showData();
           
        }catch (Exception ex)
        {
            ex.printStackTrace();
        }
    }
    
}
